/**
 * Created by user on 2017/9/21.
 */
import React, { Component } from 'react';
import { Route, Redirect } from 'react-router-dom';
import { LOGIN_PATH } from '../config';
import Storage from '../storage';
// const AuthRoute = ({needAuth:needAuth,component:Component,...rest})=>(
//     <Route {...rest} render={props => (
//     !needAuth ? (
//       <Component {...props}/>
//     ) : (
//       <Redirect to={{
//         pathname: LOGIN_PATH,
//         state: { from: props.location }
//       }}/>
//     )
//   )}/>
// );
const TestRoute = ({needAuth:needAuth,component:Component,...rest})=>(
    <Route {...rest} render={props => ( <Component {...props}/>)}/>
);
const AuthRoute = ({needAuth:needAuth,component:Component,...rest})=>{

    const renderDefault = (props)=>(<Component {...props}/>);
    const renderAuth = (props)=>{
        return (
            <Redirect
                to={{
                    pathname: LOGIN_PATH,
                    state: { from: props.location }
                }}
            />
        )
    };
    const render = (props)=>{
        const token = Storage.read('login_user');
        return needAuth && !token ? renderAuth(props) : renderDefault(props);
    };
    return (
        <Route {...rest} render={render}/>
    )
};
export default AuthRoute;